package tests;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.GregorianCalendar;

import main.Conference;
import main.Manuscript;
import main.Review;
import main.User;

import org.junit.Before;
import org.junit.Test;


import junit.framework.TestCase;

/**
 * Tests the serialized storage of a conference object
 * 
 * @author Cristopher Claeys
 * @version 12/3/11
 */
public class SerialTest extends TestCase{
	
	
  Conference conf;
  Manuscript manu1;
  Manuscript manu2;
  Review review1;
  Review review2;
  Review review3;
	
  /**
   * Store the created serializable object
   * Holly updated to support change in User
   * ID from int to String 11/21
   */
  @Before
  public void setUp() {
	manu1 = new Manuscript("manu1", "joe", "14", "text");
	manu2 = new Manuscript();
	review1 = new Review("rev", "not joe", "9", "a_text");
	review2 = new Review();
	review3 = new Review();
	manu1.addReview(review1);
	manu1.addReview(review2);
	manu2.addReview(review3);
	ArrayList<Manuscript> list = new ArrayList<Manuscript>();
	list.add(manu1);
	list.add(manu2);
	conf = new Conference("conf0", new GregorianCalendar(), 
		new GregorianCalendar(), list, 
		new ArrayList<User>());
		
	//store conf
	//start of first part of code being tested
	String filename = "conf0.ser";
	try {
	  FileOutputStream fos = new FileOutputStream(filename);
	  ObjectOutputStream out = new ObjectOutputStream(fos);
	  out.writeObject(conf);
	  out.close();
	}
	catch(IOException ex) {
      ex.printStackTrace();
	}//end of first part of code being tested
  }
	

  /**
   * Load the created object and compare the loaded result
   * against a copy already present
   */
  @Test
  public void testSerial() {
	//restore object
	//start of second part of code being tested
	String filename = "conf0.ser";
	Conference localConf = null;
	try {
	  FileInputStream fis = new FileInputStream(filename);
	  ObjectInputStream in = new ObjectInputStream(fis);
	  localConf = (Conference)in.readObject();
	  in.close();
	}
	catch(IOException ex){
      ex.printStackTrace();
	}
	catch(ClassNotFoundException ex){
	  ex.printStackTrace();
	}//end of second part of code being tested
	
	//test conference data
	assertEquals("Manuscript deadline is different", 
		localConf.getManuscriptDeadline(), 
		conf.getManuscriptDeadline());
	assertEquals("Conf name is different", 
		localConf.getConfName(), conf.getConfName());
	
	//test manuscript data
	Manuscript localManu1 = localConf.getManuscripts().get(0);
	Manuscript localManu2 = localConf.getManuscripts().get(1);
	assertEquals("Manu1 author is different", 
		localManu1.getAuthor(), manu1.getAuthor());
	assertEquals("Manu1 author ID is different", 
		localManu1.getAuthorID(), manu1.getAuthorID());
	assertEquals("Manu1 num reviews is different", 
		localManu1.getNumReviews(), manu1.getNumReviews());
	assertEquals("Manu1 title is different", 
		localManu1.getTitle(), manu1.getTitle());
	assertEquals("Manu1 file is different", 
		localManu1.getText(), manu1.getText());
	
	assertEquals("Manu2 author is different", 
		localManu2.getAuthor(), manu2.getAuthor());
	assertEquals("Manu2 author ID is different", 
		localManu2.getAuthorID(), manu2.getAuthorID());
	assertEquals("Manu2 num reviews is different", 
		localManu2.getNumReviews(), manu2.getNumReviews());
	assertEquals("Manu2 title is different", 
		localManu2.getTitle(), manu2.getTitle());
	assertEquals("Manu2 file is different", 
		localManu2.getText(), manu2.getText());
	
	//test review data
	Review localRev1 = localManu1.getReviews().get(0);
	Review localRev2 = localManu1.getReviews().get(1);
	Review localRev3 = localManu2.getReviews().get(0);
	assertEquals("Rev1 author is different", 
		localRev1.getAuthor(), review1.getAuthor());
	assertEquals("Rev1 author ID is different", 
		localRev1.getAuthorID(), review1.getAuthorID());
	assertEquals("Rev1 title is different", 
		localRev1.getTitle(), review1.getTitle());
	assertEquals("Rev1 file is different", 
		localRev1.getText(), review1.getText());
		
	assertEquals("Rev2 author is different", 
		localRev2.getAuthor(), review2.getAuthor());
	assertEquals("Rev2 author ID is different", 
		localRev2.getAuthorID(), review2.getAuthorID());
	assertEquals("Rev2 title is different", 
		localRev2.getTitle(), review2.getTitle());
	assertEquals("Rev2 file is different", 
		localRev2.getText(), review2.getText());
															
	assertEquals("Rev3 author is different", 
		localRev3.getAuthor(), review3.getAuthor());
	assertEquals("Rev3 author ID is different", 
		localRev3.getAuthorID(), review3.getAuthorID());
	assertEquals("Rev3 title is different", 
		localRev3.getTitle(), review3.getTitle());
	assertEquals("Rev3 file is different", 
		localRev3.getText(), review3.getText());
  }//end testManuInConf
}
